<?php
class Article extends AppModel {
	var $name = 'Article';
	var $belongsTo = array(
			'User' => array(
					'className' => 'User',
					'foreignKey' => 'userId',
					'fields' => 'User.name'
			));
	function getFeatures($limit = 5, $type = 'feature'){
		if($type == 'feature'){
			$order = 'Article.vote DESC';
		} else{
			$order = 'Article.since DESC';
		}
		$fields = array('Article.id','User.name','Article.title','Article.userId','Article.since');
		return $this->find('all',array('fields'=>$fields,
				'limit' => $limit,
				'order' => $order,
				'recursive' => 1
		));
	}
	function getDetail($id){
		$fields = array('Article.id','User.name','Article.title','Article.type','Article.content','Article.userId','Article.since','Article.vote');
		$conditions = array('Article.id' => $id);
		return $this->find('all',array('fields'=>$fields,
				'conditions'=> $conditions,
				'limit' => 1,
				'recursive' => 1
		));
	}
	function getList($type = 0, $limit = 50, $order = 'Article.since DESC'){
		$fields = array('Article.id','User.name','Article.title','Article.summary','Article.userId','Article.since','Article.vote');
		if($type != 0){
			$conditions = array('Article.type' => $type);
			return $this->find('all',array('fields'=>$fields,
					'conditions'=> $conditions,
					'limit' => $limit,
					'order' => $order,
					'recursive' => 1
			));
		} else {
			return $this->find('all',array('fields'=>$fields,
					'limit' => $limit,
					'order' => $order,
					'recursive' => 1
			));
		}
	}
}